<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2021-01-11 18:49:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
        :title="$t('devUpgrade.VersionToUp')"
        :visible="newVisible"
        width="40%"
        @close="()=>{$store.commit('setNewVisible',false)}"
        :close-on-click-modal='false'
        >
            <el-row :gutter="20">
                <el-col :span="17" :offset="2">
                    <el-form ref="form" :model="form" label-width="120px" >
                        <el-form-item  :label="$t('version.versionAdd.versionNum')" prop="version" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.version" filterable :placeholder="$t('please_choose')" clearable style="width:100%;" >
                                <el-option
                                    v-for="item in versionArray"
                                    :key="item.taskId"
                                    :label="item.longVersion"
                                    :value="item.taskId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisible',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {

    },
    props: {
        wifiSnArray:{
            type:Array,
            default:()=>{return []}
        },
        armArry:{
            type:Array,
            default:()=>{return []}
        },mkArry:{
            type:Array,
            default:()=>{return []}
        },dspArry:{
            type:Array,
            default:()=>{return []}
        },versionArray:{
            type:Array,
            default:()=>{return []}
        },
        inverterType:{
            type:String,
            default:''
        },
        scope:{
            type:String,
            default:''
        }
    },
    data() {
        return {
            form:{
                version:"",
                type:"",
            },
            versionList:[]
        };
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {
    },
    created() {

    },
    mounted() {
    },
    methods: {
        async typeChange(){
            if(!this.form.type){
                return
            }
            let res = await this.API.deviceTestGetUpVersion({
                wifiSN:this.editWifiSN,
                type:this.form.type,
            })
            this.versionList = res
        },
        async deviceTestUpdateVersion(){
            let taskId=this.form.version;
            let wifiSns='';

            let inverterType=this.inverterType;
            let scope=this.scope;
            let checkedVersion = this.comm.returnCode(this.versionArray,this.form.version,'taskId','longVersion')
            for (var i = 0; i < this.wifiSnArray.length; i++) {
                if(this.inverterType==1){
					if(this.armArry[i]==checkedVersion){
						continue;
					}
				}else if(this.inverterType==2){
					if(this.mkArry[i]==checkedVersion){
						continue;
					}
				}else if(this.inverterType==3){
					
					if(this.dspArry[i]==checkedVersion){
						continue;
					}
                }
                if(wifiSns.length==0){
					wifiSns+=taskId+'#'+wifiSnArray[i];
					checkWifi=wifiSnArray[i];
				}else{
					wifiSns+=";"+taskId+'#'+wifiSnArray[i];
					checkWifi=','+wifiSnArray[i];
				}
            }
            
            let res = await this.API.deviceTestUpdateVersion({
                wifiSns:wifiSns,
                inverterType:this.inverterType,
                scope:this.scope,
            })
            if(res.success){
                this.comm.showMsg(true,this.$t('devUpgrade.waitForUpgrade'))
                this.$emit('uploadData',true)
            }else{
                this.comm.showMsg(false,this.$t('devUpgrade.upgradeFail'))
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.deviceTestUpdateVersion()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
